 /*
  * Copyright (c) 2021, 2023, yishun.cn All rights reserved.
  *
  */

 import cn.shun.DBMapper;
 import org.apache.ibatis.datasource.pooled.PooledDataSource;
 import org.apache.ibatis.mapping.Environment;
 import org.apache.ibatis.session.Configuration;
 import org.apache.ibatis.session.SqlSession;
 import org.apache.ibatis.session.SqlSessionFactory;
 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 import org.apache.ibatis.transaction.TransactionFactory;
 import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.DisplayName;
 import org.junit.jupiter.api.Test;

 import javax.sql.DataSource;
 import java.sql.Connection;


 /**
  * <p>Project: MyBatis2023 - MybatisDemo
  * <p>Powered by yishun On 2023-01-18 20:05:12
  *
  * @author yishun [2465734101@qq.com]
  * @version 1.0
  * @since 17
  */
 @DisplayName("mybatis demo 全编程案例")
 public class MybatisDemo {
     DBMapper db;

     @BeforeEach
     void init() {
         //建立数据连接池
         DataSource ds = new PooledDataSource("com.mysql.cj.jdbc.Driver", "jdbc:mysql://localhost:3306/dbtest", "root", "123456");
         //事务对象
         TransactionFactory tx = new JdbcTransactionFactory();
         //开发环境对象
         Environment env = new Environment("development", tx, ds);
         //建立配置文件
         Configuration cfg = new Configuration(env);
         cfg.addMapper(DBMapper.class);
         //建立SqlSessionFactory
         SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(cfg);
         SqlSession session = sf.openSession();
         db = session.getMapper(DBMapper.class);
     }

     @Test
     void t1() {
         System.out.println(db.version());
         System.out.println(db.tbs());
         System.out.println(db.queryAll());
     }
 }
